var prefix = "/system/loanApply"
$(function () {
    load();
});

//转换日期格式(时间戳转换为datetime格式)
function changeDateFormat(cellval) {
    var dateVal = cellval + "";
    if (cellval != null) {
        var date = new Date(parseInt(dateVal.replace("/Date(", "").replace(")/", ""), 10));
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

        return date.getFullYear() + "-" + month + "-" + currentDate + " " + hours + ":" + minutes + ":" + seconds;
    }
}

function load() {
    $('#exampleTable')
        .bootstrapTable(
            {
                method: 'get', // 服务器数据的请求方式 get or post
                url: prefix + "/list", // 服务器数据的加载地址
                //	showRefresh : true,
                //	showToggle : true,
                //	showColumns : true,
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: true, // 设置为true会有隔行变色效果
                dataType: "json", // 服务器返回的数据类型
                pagination: true, // 设置为true会在底部显示分页条
                // queryParamsType : "limit",
                // //设置为limit则会发送符合RESTFull格式的参数
                singleSelect: false, // 设置为true将禁止多选
                // contentType : "application/x-www-form-urlencoded",
                // //发送到服务器的数据编码类型
                pageSize: 10, // 如果设置了分页，每页数据条数
                pageNumber: 1, // 如果设置了分布，首页页码
                //search : true, // 是否显示搜索框
                showColumns: false, // 是否显示内容下拉框（选择显示的列）
                sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
                queryParams: function (params) {
                    return {
                        //说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
                        limit: params.limit,
                        offset: params.offset,
                        remark8: $('#searchShop').val().trim(),
                        remark6: $('#searchPhone').val().trim(),
                        realName: $('#searchName').val().trim(),
                        startDate: $('#startDate').val().trim(),
                        endDate: $('#endDate').val().trim(),
                        status: 4
                    };
                },
                // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
                // queryParamsType = 'limit' ,返回参数必须包含
                // limit, offset, search, sort, order 否则, 需要包含:
                // pageSize, pageNumber, searchText, sortName,
                // sortOrder.
                // 返回false将会终止请求
                columns: [
                    {
                        checkbox: true
                    },
                    /*
                    {
                        field : 'id',
                        title : '申请ID'
					},*/
                    {
                        field: 'remark6',
                        align: 'center',
                        title: '手机号码'
                    },
                    {
                        field: 'realName',
                        align: 'center',
                        title: '真实姓名',
                        formatter: function (value, row) {
                            if (value == null || value == "") {
                                return "未实名";
                            } else {
                                return value;
                            }
                        }
                    },
                    /*								{
                        field : 'createTime',
                        title : '时间',
                        align : 'center',
                        valign : 'center',
                        formatter: function (value, row) {
                            return changeDateFormat(value)
                        }
                    },*/
                    {
                        field: 'userInfoAuth',
                        align: 'center',
                        title: '信息认证',
                        formatter: function (item, index) {
                            //是否锁定 0 未锁定，1锁定
                            if (item == '0' || item == null || item == "") {
                                return '<span class="label label-warning">未认证</span>';
                            } else if (item == '2') {
                                return '<span class="label label-success">已完善</span>';
                            }
                        }
                    },
                    {
                        field: 'contactAuth',
                        align: 'center',
                        title: '联系人认证',
                        formatter: function (item, index) {
                            //是未认证锁定 0 未锁定，1锁定
                            if (item == '0' || item == null || item == "") {
                                return '<span class="label label-warning">未认证</span>';
                            } else if (item == '2') {
                                return '<span class="label label-success">已完善</span>';
                            }
                        }
                    },
                    {
                        field: 'bankAuth',
                        align: 'center',
                        title: '银行卡认证',
                        formatter: function (item, index) {
                            //是未认证锁定 0 未锁定，1锁定
                            if (item == '0' || item == null || item == "") {
                                return '<span class="label label-warning">未认证</span>';
                            } else if (item == '2') {
                                return '<span class="label label-success">已认证</span>';
                            }
                        }
                    },
                    {
                        field: 'faceAuth',
                        align: 'center',
                        title: '人脸认证',
                        formatter: function (item, index) {
                            //是未认证锁定 0 未锁定，1锁定
                            if (item == '0' || item == null || item == "") {
                                return '<span class="label label-warning">未认证</span>';
                            } else if (item == '2') {
                                return '<span class="label label-success">已认证</span>';
                            } else if (item == '1') {
                                return '<span class="label label-primary">审核中</span>';
                            } else if (item == '5') {
                                return '<span class="label label-primary">审核不通过</span>';
                            } else if (item == '3') {
                                return '<span class="label label-danger">认证失败</span>';
                            }
                        }
                    },
                    {
                        field: 'creditAuth',
                        align: 'center',
                        title: '运营商认证',
                        formatter: function (item, index) {
                            //是否锁定 0 未锁定，1锁定
                            if (item == '0' || item == null || item == "") {
                                return '<span class="label label-warning">未获取</span>';
                            } else if (item == '2') {
                                return '<span class="label label-success">已获取</span>';
                            }
                        }
                    },
                    /*								{
                        field : 'applyAmount',
                        title : '最近借款申请金额'
                    },
                                                    {
                        field : 'loanAmount',
                        title : '最近借款放款金额'
                    },*/
                    /*								{
                        field : 'loanProductName',
                        title : '贷款产品'
                    },
                                                    {
                        field : 'loanProductId',
                        title : '贷款产品ID'
                    },
                                                    {
                        field : 'loanPeriod',
                        title : '贷款期间'
                    },
                                                    {
                        field : 'loanRate',
                        title : '贷款利率'
                    },*/

                    /*								{
                        field : 'loanScore',
                        title : '借贷评级'
                    },
                                                    {
                        field : 'remark1',
                        title : '备注'
                    },
                                                    {
                        field : 'remark2',
                        title : '备注'
                    },
                                                    {
                        field : 'remark3',
                        title : '备注'
                    },
                                                    {
                        field : 'remark4',
                        title : '备注'
                    },*/
                    {
                        field: 'remark5',
                        title: '放款时间'
                    },
                    {
                        field: 'billingtype',
                        align: 'center',
                        title: '支付宝认证',
                        formatter: function (item, index) {
                            if (item == '0' || item == null || item == "") {
                                return '<span class="label label-warning">未认证</span>';
                            } else if (item == '2') {
                                return '<span class="label label-success">已认证</span>';
                            }
                        }
                    },
                    {
                        field: 'billingtypeCard',
                        align: 'center',
                        title: '信用卡认证',
                        formatter: function (item, index) {
                            if (item == '0' || item == null || item == "") {
                                return '<span class="label label-warning">未认证</span>';
                            } else if (item == '2') {
                                return '<span class="label label-success">已认证</span>';
                            }
                        }
                    },
                    {
                        field: 'status',
                        align: 'center',
                        title: '最近借款状态',
                        formatter: function (item, index) {
                            //借款状态 0 申请中, 1 待放款,2，放款中，3已放款  4，还款中，5已还清
                            if (item == '0' || item == null || item == "") {
                                return '<span class="label label-warning">申请中</span>';
                            } else if (item == '1') {
                                return '<span class="label label-primary">待放款</span>';
                            } else if (item == '2') {
                                return '<span class="label label-primary">放款中</span>';
                            } else if (item == '3') {
                                return '<span class="label label-primary">已放款</span>';
                            } else if (item == '4') {
                                return '<span class="label label-info">还款中</span>';
                            } else if (item == '5') {
                                return '<span class="label label-success">已还清</span>';
                            } else if (item == '6') {
                                return '<span class="label label-danger">已拒绝</span>';
                            }

                        }
                    },
                    {
                        field: 'remark8',
                        align: 'center',
                        title: '系统名称',
                        formatter: function (item, index) {
                            return '<span style="color:#0c52ef">' + item + '</span>';
                        }
                    },
                    {
                        title: '操作',
                        field: 'id',
                        align: 'left',
                        formatter: function (value, row, index) {
                            var a = '<a class="btn btn-primary btn-sm ' + s_edit_h + '" href="#" mce_href="#" title="风盾个人信息" onclick="userInfo(\''
                                + row.userId
                                + '\')"><i class="fa fa-user"></i></a> ';
                            var k = '<a class="btn btn-primary btn-sm ' + s_edit_h + '"  target=" _blank"  href="/system/loanJuxinliReport/honeyJar/' + row.userId + '" mce_href="#" title="风盾JXLMG"><i class="fa fa-glass"></i></a> ';
                            var m = '<a class="btn btn-primary btn-sm ' + s_edit_h + '"  target=" _blank"  href="/system/loanJuxinliMMobile/mobileInfo/' + row.userId + '" mce_href="#" title="通话记录"><i class="fa fa-phone"></i></a> ';
                            var e = '<a class="btn btn-primary btn-sm ' + s_edit_h + '" href="#" mce_href="#" title="审批金额" onclick="edit(\''
                                + row.id
                                + '\')"><i class="fa fa-edit"></i></a> ';

                            var f = '<a class="btn btn-success btn-sm" href="#" title="详情"  mce_href="#" onclick="details(\''
                                + row.id
                                + '\')"><i class="fa fa-list"></i></a> ';
                            var g = '<a class="btn btn-success btn-sm" href="#" title="通过【是否已还清】"  mce_href="#" onclick="pass(\''
                                + row.id
                                + '\')"><i class="fa fa-forward"></i></a> ';
                            var x = '<br> <a class="btn btn-success btn-sm" href="#" title="驳回"  mce_href="#" onclick="noPass(\''
                                + row.id
                                + '\')"><i class="fa fa-backward"></i></a> ';

                            var y = '<a class="btn btn-success btn-sm" href="#" title="清空"  mce_href="#" onclick="clear1(\''
                                + row.id
                                + '\',\'' + row.userId + '\')"><i class="fa fa-remove"></i></a> ';

                            var z = '<a class="btn btn-success btn-sm" href="#" title="拒绝"  mce_href="#" onclick="refuse(\''
                                + row.id
                                + '\',\'' + row.userId + '\')"><i class="fa fa-user-times"></i></a> ';

                            var mm = '<a class="btn btn-primary btn-sm ' + s_edit_h + '"  target=" _blank"  href="/system/loanHuadaoInfo/huaDaoInfo/' + row.userId + '" mce_href="#" title="风盾HDXX"><i class="fa fa-bank"></i></a> ';

                            if (row.status == '6') {
                                return a + k + m + y + f + mm;
                            } else {
                                return e + g + f + mm;
                            }
                        }
                    }],
                onLoadSuccess: function (data) {  //加载成功时执行
                    if (data.rows[0] != null && data.rows[0] != 'undefined' && data.total != 0 && 1 != data.rows[0].hasShowSysName) {
                        $('#exampleTable').bootstrapTable('hideColumn', 'remark8');
                    }
                }
            });
}

function reLoad() {
    $('#exampleTable').bootstrapTable('refresh');
}

function add() {
    layer.open({
        type: 2,
        title: '增加',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['800px', '520px'],
        content: prefix + '/add' // iframe的url
    });
}

function details(id) {
    layer.open({
        type: 2,
        title: '详情',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['800px', '520px'],
        content: prefix + '/details/' + id // iframe的url
    });
}

function edit(id) {
    layer.open({
        type: 2,
        title: '编辑',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['360px', '360px'],
        content: prefix + '/edit/' + id // iframe的url
    });
}


function honeyJar(id) {
    layer.open({
        type: 2,
        title: '风盾JXLMG',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['100%', '100%'],
        content: '/system/loanJuxinliReport/honeyJar/' + id // iframe的url
    });
}

function userInfo(userId) {
    layer.open({
        type: 2,
        title: '风盾个人信息',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['1000px', '520px'],
        content: '/system/loanUser/details/' + userId // iframe的url
    });
}

function huaDaoInfo(userId) {
    layer.open({
        type: 2,
        title: '风盾HDXX',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['1000px', '520px'],
        content: '/system/loanJuxinliMMobile/huaDaoInfo/' + userId // iframe的url
    });
}

function remove(id) {
    layer.confirm('确定要删除选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/remove",
            type: "post",
            data: {
                'id': id
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function pass(id) {
    layer.confirm('确定要审核通过选中的借款？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/pass",
            type: "post",
            data: {
                'id': id
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}


function noPass(id) {
    layer.confirm('确定此借款要驳回到上一级？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/noPass",
            type: "post",
            data: {
                'id': id
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function clear1(id, userId) {
    layer.confirm('确定此借款要清空吗？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/clear",
            type: "post",
            data: {
                'id': id,
                'userId': userId
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function refuse(id, userId) {
    layer.confirm('确定此借款要拒绝吗？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/refuse",
            type: "post",
            data: {
                'id': id,
                'userId': userId
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function resetPwd(id) {
}

function batchRemove() {
    var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
    if (rows.length == 0) {
        layer.msg("请选择要删除的数据");
        return;
    }
    layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
        btn: ['确定', '取消']
        // 按钮
    }, function () {
        var ids = new Array();
        // 遍历所有选择的行数据，取每条数据对应的ID
        $.each(rows, function (i, row) {
            ids[i] = row['id'];
        });
        $.ajax({
            type: 'POST',
            data: {
                "ids": ids
            },
            url: prefix + '/batchRemove',
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    }, function () {

    });
}